const Base = require('./base.js');
const { think } = require('thinkjs');
const moment = require('moment');

module.exports = class extends Base {
  async bannerAction() {
    const model = this.model('banner');
    const data = await model.order(['sort DESC']).where({type:1}).select();

    return this.success(data);
  }
  async setAction() {
    const model = this.model('set');
    const data = await model.find();
    return this.success(data);
  }
  async tagsAction() {
    const model = this.model('tags');
    const data = await model.where({recommend: 1}).order(['sort DESC']).page(1, 6).countSelect();

    return this.success(data);
  }
  async tagAllAction() {
    const model = this.model('tags');
    const data = await model.order(['id DESC']).select();

    return this.success(data);
  }

  async tagCommendAction(){
    const model = this.model('tags');
    const data = await model.where({recommend: 1}).order(['id DESC']).select();

    return this.success(data);
  }
  async figureIndexAction(){
    const model = this.model('figure');
    const query = this.get()
    query.show = 1
    query.recommend = 1

    // await this.model('user').where({status: 1, id: this.getLoginUserId()}).find()

    const data = await model.where(query).field(['name', 'is_vip', 'id', 'avatar', 'company_name', 'position_name', 'city_name', 'info']).order(['sort DESC']).page(1, 12).countSelect();
    const all = await model.count('id');

    return this.success({data: data, count: all});
  }
  async figureListAction(){
    const model = this.model('figure');
    const query = this.get()

    query.show = 1
    
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    if (query.name) {
      query['position|name|company_name'] = ['like', [`%${query.name}%`]]
      delete query.name
    }

    if(query.tag){
      query.tag = ['like', `%${query.tag}%`]
    }


    delete query.page

    const data = await model.where(query).order(['sort DESC']).field(['name', 'is_vip', 'id', 'avatar', 'company_name', 'position_name', 'city_name', 'info']).page(page, size).countSelect();

    return this.success(data);
  }
  async positionAction() {
    const model = this.model('figure');
    const data = await model.where({position: ['!=', '']}).field('position').distinct('position').select()
    return this.success(data);
  }
  async industryAction() {
    const model = this.model('figure');
    const data = await model.where({industry: ['!=', '']}).field('industry').distinct('industry').select()
    return this.success(data);
  }
  async identityAction() {
    const model = this.model('figure');
    const data = await model.where({identity: ['!=', '']}).field('identity').distinct('identity').select()
    return this.success(data);
  }
  async detailAction() {
    const id = this.get('id')
    const model = this.model('figure');
    const data = await model.where({id: id}).find();

    return this.success(data);
  }
  async applyAction(){
    let count = await this.model('apply').where({create_time: ['between', `${moment().startOf('day').format("YYYY-MM-DD HH:mm:ss")}, ${moment().endOf('day').format("YYYY-MM-DD HH:mm:ss")}`], user_id: this.getLoginUserId()}).select();
    if(!think.isEmpty(count)){
      const has_apply = count.some(item=>{
        return item.figture == this.get('id')
      })
      if(!think.isEmpty(has_apply)){
        return this.fail(409, '已申请成功，无需重复申请，请耐心等待联系');
      }else{
        const num = await this.model('set').find();
        if(count.length >= num.num){
          return this.fail(409, '超出当日限制');
        }
      }

    }

    const user = await this.model('user').where({id: this.getLoginUserId()}).find()
    const data = {
      user_name: user.name,
      company: user.company,
      position: user.position,
      figture: this.get('id'),
      user_id: this.getLoginUserId(),
      create_time: ['exp', 'CURRENT_TIMESTAMP()']
    }
    await this.model('apply').add(data)
    return this.success();
  }
};
